package redecolaborativa.client;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;

public class Perfil extends Composite {
	private String visualizador, visualizado, idSessao;
	private AbsolutePanel absolutePanel;
	private FachadaAsync fachada;
	private AbsolutePanel absolutePanel_1, absolutePanel_2;
	private ScrollPanel painelGeral, painelItens;
	
	public Perfil(FachadaAsync fachada, ScrollPanel painelGeral, final String idSessao, final String visualizador, final String visualizado) {
		this.fachada = fachada;
		this.visualizado = visualizado;
		this.visualizador = visualizador;
		this.idSessao = idSessao;
		this.painelGeral = painelGeral;
		this.absolutePanel_1 = new AbsolutePanel();
		this.absolutePanel_2 = new AbsolutePanel();
		this.painelItens = new ScrollPanel();
	
		absolutePanel = new AbsolutePanel();
		initWidget(absolutePanel);
		absolutePanel.setSize("847px", "487px");
		
		Label lblNewLabel = new Label(visualizado);
		absolutePanel.add(lblNewLabel, 74, 29);
		
		CaptionPanel cptnpnlNewPanel = new CaptionPanel("Amigos");
		absolutePanel.add(cptnpnlNewPanel, 26, 66);
		cptnpnlNewPanel.setSize("215px", "377px");
		
		ScrollPanel scrollPanel = new ScrollPanel();
		cptnpnlNewPanel.setContentWidget(scrollPanel);
		scrollPanel.setSize("214px", "359px");
		
		scrollPanel.setWidget(absolutePanel_1);
		absolutePanel_1.setSize("100%", "100%");
		
		CaptionPanel cptnpnlNewPanel_1 = new CaptionPanel("Itens");
		absolutePanel.add(cptnpnlNewPanel_1, 300, 101);
		cptnpnlNewPanel_1.setSize("499px", "342px");
		
		cptnpnlNewPanel_1.setContentWidget(painelItens);
		painelItens.setSize("499px", "332px");
		painelItens.setWidget(absolutePanel_2);
		absolutePanel_2.setSize("100%", "100%");
		
		adicionarAmigo();
		mostraAmigos();
		mostraItens();
	}

	private void adicionarAmigo() {
		fachada.apareceBotao(visualizador, visualizado, new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				if(result.booleanValue()) {
					Button btnAdicionarAmigo = new Button("Adicionar Amigo");
					btnAdicionarAmigo.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							try {
								fachada.requisitarAmizade(idSessao, visualizado, new AsyncCallback<Void>() {
									@Override
									public void onSuccess(Void result) {
										Window.alert("Solicitacao enviada com sucesso!");
									}
									@Override
									public void onFailure(Throwable caught) {
										Window.alert("Solicitacao enviada com sucesso!");
									}
								});
							} catch (Exception e) {
								Window.alert(e.getMessage());
							}
						}
					});
					absolutePanel.add(btnAdicionarAmigo, 663, 66);
					btnAdicionarAmigo.setSize("154px", "28px");
				}
			}
			@Override
			public void onFailure(Throwable caught) { }
		}); 
	}
	
	private void mostraAmigos() {
		try {
			fachada.getAmigos(idSessao, visualizado, new AsyncCallback<List<String>>() {
				@Override
				public void onSuccess(List<String> result) {
					for(final String str : result) {
						String nome = str.split("-")[0];
						final Label amigo = new Label(nome);
						absolutePanel_1.add(amigo);
						amigo.addClickHandler(new ClickHandler() {
							@Override
							public void onClick(ClickEvent event) {
								painelGeral.clear();
								painelGeral.add(new Perfil(fachada, painelGeral, idSessao, visualizador, str.split("-")[1]));
							}
						});
						eventoDeMouseOver(amigo);
					}
				}
				@Override
				public void onFailure(Throwable caught) {
					Window.alert(caught.getMessage());
				}
			});
		} catch (Exception e) {
			Window.alert(e.getMessage());
		}
	}
	
	private void eventoDeMouseOver(final Label label) {
		label.addMouseOverHandler(new MouseOverHandler() {
			@Override
			public void onMouseOver(MouseOverEvent event) {
				label.setStyleName("underlined");
			}
		});
		label.addMouseOutHandler(new MouseOutHandler() {
			@Override
			public void onMouseOut(MouseOutEvent event) {
				label.setStyleName("gwt-Label");								
			}
		});
	}
	
	private void mostraItens() {
		fachada.getItens(visualizado, new AsyncCallback<List<String>>() {
			@Override
			public void onSuccess(List<String> result) {
				mostraResultados(result);
			}
			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());				
			}
		});
	}
	
	private void mostraResultados(List<String> result) {
		for (final String item: result) {
			String nomeItem = item.split("-")[0];
			String desc = item.split("-")[1];
			String cat = item.split("-")[2];
			String dono = item.split("-")[3];
			String notaItem = item.split("-")[4];
			
			Label itemClick = new Label("Item: " + nomeItem + "      Categoria: " + cat);
			Label descricaoItem = new Label("Descricao: " + desc);
			Label donoItem = new Label("Dono: " + dono);
			Label ultimaNota = new Label("Ultima nota atribuida ao item: " + notaItem);
			
			eventoDeMouseOver(itemClick);
			absolutePanel_2.add(itemClick);
			absolutePanel_2.add(descricaoItem);
			absolutePanel_2.add(donoItem);
			absolutePanel_2.add(ultimaNota);
			
			itemClick.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					painelItens.clear();
					painelItens.add(new VisualizarItem(painelGeral, visualizador, item, idSessao, item.split("-")[0], fachada));
				}
			});
		} 
	}
}